from selenium import webdriver
import time
import DBUtils


class TestBankCadr:

    # def test_bankcard1(self):
    #
    #     driver = webdriver.Chrome()
    #
    #     driver.get(url='http://localhost:90')
    #
    #     driver.maximize_window()
    #
    #     driver.find_element_by_xpath('//input[@id="username"]').send_keys("lisi")
    #
    #     driver.find_element_by_xpath('//input[@id="password"]').send_keys("123456")
    #
    #     driver.find_element_by_xpath('//button[@id="login_btn"]').click()
    #
    #     time.sleep(2)
    #
    #     driver.find_element_by_xpath('//nav[@class="sidebar-main"]/ul/li[4]').click()
    #
    #     time.sleep(1)
    #
    #     driver.find_element_by_xpath('//nav[@class="sidebar-main"]/ul/li[4]/ul/li[3]/a').click()
    #     # 点击银行卡管理
    #     time.sleep(2)
    #
    #     driver.find_element_by_xpath('//button[@id="bankCard_add_modal_btn"]').click()
    #
    #     time.sleep(1)
    #
    #     driver.find_element_by_xpath('//input[@id="cardBank_add_input"]').send_keys('伊朗中央银行')
    #
    #     driver.find_element_by_xpath('//input[@id="cardNum_add_input"]').send_keys('1234567891548796')
    #
    #     driver.find_element_by_xpath('//button[@id="bankCard_save_btn"]').click()
    #
    #     time.sleep(5)
    #
    #     result2 = driver.find_element_by_xpath("//div[@class='card-body']/h4[2]").text
    #     # 账户余额：20116.20元
    #     result = result2[result2.find('：')+1:result2.find('元')]
    #
    #     sql = 'select * from bankcard where cardBank = %s and cardNum = %s'
    #     params = ['伊朗中央银行','1234567891548796']
    #     data = DBUtils.select(sql,params)
    #
    #     if result == '10000.00' and len(data) == 1:
    #         print('通过')
    #     else:
    #         print('不通过')
    #         driver.save_screenshot('test_bankcard1.png')
    #
    #     assert result == '10000.00' and len(data) == 1
    #
    #     driver.quit()
    #
    #     sql = 'delete from bankcard where cardBank = %s and cardNum = %s'
    #     params = ['伊朗中央银行', '1234567891548796']
    #     DBUtils.update(sql,params)


    # def test_bankcard2(self):
    #
    #     driver = webdriver.Chrome()
    #
    #     driver.get(url='http://localhost:90')
    #
    #     driver.maximize_window()
    #
    #     driver.find_element_by_xpath('//input[@id="username"]').send_keys("lisi")
    #
    #     driver.find_element_by_xpath('//input[@id="password"]').send_keys("123456")
    #
    #     driver.find_element_by_xpath('//button[@id="login_btn"]').click()
    #
    #     time.sleep(2)
    #
    #     driver.find_element_by_xpath('//nav[@class="sidebar-main"]/ul/li[4]').click()
    #
    #     time.sleep(1)
    #
    #     driver.find_element_by_xpath('//nav[@class="sidebar-main"]/ul/li[4]/ul/li[3]/a').click()
    #     # 点击银行卡管理
    #     time.sleep(2)
    #
    #     driver.find_element_by_xpath('//button[@id="bankCard_add_modal_btn"]').click()
    #
    #     time.sleep(1)
    #
    #     driver.find_element_by_xpath('//input[@id="cardBank_add_input"]').send_keys('伊拉克国民银行')
    #
    #     driver.find_element_by_xpath('//input[@id="cardNum_add_input"]').send_keys('12345678915487967')
    #
    #     driver.find_element_by_xpath('//button[@id="bankCard_save_btn"]').click()
    #
    #     time.sleep(5)
    #
    #     bankCards = driver.find_elements_by_xpath("//div[@class='container-fluid']/div/div[not(@class)]")
    #     # 银行卡的数量
    #
    #     if len(bankCards) == 0:
    #         print('通过')
    #     else:
    #         print('不通过')
    #
    #     driver.quit()
    #
    #     assert len(bankCards) == 0
    #
    #     sql = 'delete from bankcard where cardBank = %s and cardNum = %s'
    #     params = ['伊拉克国民银行', '12345678915487967']
    #     DBUtils.update(sql,params)


    # def test_bankcard3(self):
    #
    #     driver = webdriver.Chrome()
    #
    #     driver.get(url='http://localhost:90')
    #
    #     driver.maximize_window()
    #
    #     driver.find_element_by_xpath('//input[@id="username"]').send_keys("lisi")
    #
    #     driver.find_element_by_xpath('//input[@id="password"]').send_keys("123456")
    #
    #     driver.find_element_by_xpath('//button[@id="login_btn"]').click()
    #
    #     time.sleep(2)
    #
    #     driver.find_element_by_xpath('//nav[@class="sidebar-main"]/ul/li[4]').click()
    #
    #     time.sleep(1)
    #
    #     driver.find_element_by_xpath('//nav[@class="sidebar-main"]/ul/li[4]/ul/li[3]/a').click()
    #     # 点击银行卡管理
    #     time.sleep(2)
    #
    #     driver.find_element_by_xpath('//button[@id="bankCard_add_modal_btn"]').click()
    #
    #     time.sleep(1)
    #
    #     driver.find_element_by_xpath('//input[@id="cardBank_add_input"]').send_keys('伊拉克国民银行')
    #
    #     driver.find_element_by_xpath('//input[@id="cardNum_add_input"]').send_keys('1234567891548796')
    #
    #     driver.find_element_by_xpath('//button[@id="bankCard_save_btn"]').click()
    #
    #     time.sleep(5)
    #
    #     bankCards = driver.find_elements_by_xpath("//div[@class='container-fluid']/div/div[not(@class)]")
    #     # 银行卡的数量
    #
    #     if len(bankCards) == 1:
    #         # 原本那张银行卡
    #         print('通过')
    #     else:
    #         print('不通过')
    #         driver.save_screenshot('test_bankcard3.png')
    #
    #     driver.quit()
    #
    #     assert len(bankCards) == 1


    # def test_bankcard4(self):
    #
    #     driver = webdriver.Chrome()
    #
    #     driver.get(url='http://localhost:90')
    #
    #     driver.maximize_window()
    #
    #     driver.find_element_by_xpath('//input[@id="username"]').send_keys("lisi")
    #
    #     driver.find_element_by_xpath('//input[@id="password"]').send_keys("123456")
    #
    #     driver.find_element_by_xpath('//button[@id="login_btn"]').click()
    #
    #     time.sleep(2)
    #
    #     driver.find_element_by_xpath('//nav[@class="sidebar-main"]/ul/li[4]').click()
    #
    #     time.sleep(1)
    #
    #     driver.find_element_by_xpath('//nav[@class="sidebar-main"]/ul/li[4]/ul/li[3]/a').click()
    #     # 点击银行卡管理
    #     time.sleep(2)
    #
    #     driver.find_element_by_xpath('//button[@id="bankCard_add_modal_btn"]').click()
    #
    #     time.sleep(1)
    #
    #     driver.find_element_by_xpath('//input[@id="cardBank_add_input"]').send_keys('伊拉克国民银行')
    #
    #     ele = driver.find_element_by_xpath("//div[@id='bankCardAddModal']//div[@class='modal-body']/form/div[2]/div[1]/div/label[2]/span")
    #     # 选择信用卡
    #     # print(ele.text)
    #     time.sleep(1)
    #
    #     ele.click()
    #
    #     driver.find_element_by_xpath('//input[@id="cardNum_add_input"]').send_keys('1234567891548796')
    #
    #     driver.find_element_by_xpath('//button[@id="bankCard_save_btn"]').click()
    #
    #     time.sleep(5)
    #
    #     bankCards = driver.find_elements_by_xpath("//div[@class='container-fluid']/div/div[not(@class)]")
    #     # 银行卡的数量
    #
    #     if len(bankCards) == 1:
    #         # 原本那张银行卡
    #         print('通过')
    #     else:
    #         print('不通过')
    #         driver.save_screenshot('test_bankcard4.png')
    #
    #     driver.quit()
    #
    #     assert len(bankCards) == 1


    # def test_bankcard5(self):
    #
    #     driver = webdriver.Chrome()
    #
    #     driver.get(url='http://localhost:90')
    #
    #     driver.maximize_window()
    #
    #     driver.find_element_by_xpath('//input[@id="username"]').send_keys("lisi")
    #
    #     driver.find_element_by_xpath('//input[@id="password"]').send_keys("123456")
    #
    #     driver.find_element_by_xpath('//button[@id="login_btn"]').click()
    #
    #     time.sleep(2)
    #
    #     driver.find_element_by_xpath('//nav[@class="sidebar-main"]/ul/li[4]').click()
    #
    #     time.sleep(1)
    #
    #     driver.find_element_by_xpath('//nav[@class="sidebar-main"]/ul/li[4]/ul/li[3]/a').click()
    #     # 点击银行卡管理
    #     time.sleep(2)
    #
    #     cards = driver.find_elements_by_xpath("//div[@class='row']/div[not(@class)]/div")
    #     # 银行卡的数量
    #     cardNums = len(cards)
    #
    #     cards[0].find_element_by_xpath('./div[@class="card"]//ul[@class="card-actions"]/li/button[contains(@class,"delete_btn")]').click()
    #     # 对第一张银行卡执行删除操作
    #     time.sleep(2)
    #
    #     driver.find_element_by_xpath("//div[@class='jconfirm-buttons']/button[1]").click()
    #     # 点击确认按钮
    #
    #     time.sleep(3)
    #
    #     cur_cards = driver.find_elements_by_xpath("//div[@class='row']/div[not(@class)]/div")
    #     # 统计银行卡数量v
    #
    #     if len(cur_cards) + 1 == cardNums:
    #         print('通过')
    #     else:
    #         print('不通过')
    #
    #     assert len(cur_cards) + 1 == cardNums
    #
    #     driver.quit()


    # def test_bankcard6(self):
    #
    #     driver = webdriver.Chrome()
    #
    #     driver.get(url='http://localhost:90')
    #
    #     driver.maximize_window()
    #
    #     driver.find_element_by_xpath('//input[@id="username"]').send_keys("lisi")
    #
    #     driver.find_element_by_xpath('//input[@id="password"]').send_keys("123456")
    #
    #     driver.find_element_by_xpath('//button[@id="login_btn"]').click()
    #
    #     time.sleep(2)
    #
    #     driver.find_element_by_xpath('//nav[@class="sidebar-main"]/ul/li[4]').click()
    #
    #     time.sleep(1)
    #
    #     driver.find_element_by_xpath('//nav[@class="sidebar-main"]/ul/li[4]/ul/li[3]/a').click()
    #     # 点击银行卡管理
    #     time.sleep(2)
    #
    #     cards = driver.find_elements_by_xpath("//div[@class='row']/div[not(@class)]/div")
    #     # 银行卡的数量
    #     cardNums = len(cards)
    #
    #     cards[0].find_element_by_xpath('./div[@class="card"]//ul[@class="card-actions"]/li/button[contains(@class,"delete_btn")]').click()
    #     # 对第一张银行卡执行删除操作
    #     time.sleep(2)
    #
    #     driver.find_element_by_xpath("//div[@class='jconfirm-buttons']/button[2]").click()
    #     # 点击取消按钮
    #
    #     time.sleep(3)
    #
    #     cur_cards = driver.find_elements_by_xpath("//div[@class='row']/div[not(@class)]/div")
    #     # 统计银行卡数量
    #
    #     if len(cur_cards) == cardNums:
    #         print('通过')
    #     else:
    #         print('不通过')
    #
    #     assert len(cur_cards) == cardNums
    #
    #     driver.quit()


    # def test_bankcard7(self):
    #
    #     driver = webdriver.Chrome()
    #
    #     driver.get(url='http://localhost:90')
    #
    #     driver.maximize_window()
    #
    #     driver.find_element_by_xpath('//input[@id="username"]').send_keys("lisi")
    #
    #     driver.find_element_by_xpath('//input[@id="password"]').send_keys("123456")
    #
    #     driver.find_element_by_xpath('//button[@id="login_btn"]').click()
    #
    #     time.sleep(2)
    #
    #     driver.find_element_by_xpath('//nav[@class="sidebar-main"]/ul/li[4]').click()
    #
    #     time.sleep(1)
    #
    #     driver.find_element_by_xpath('//nav[@class="sidebar-main"]/ul/li[4]/ul/li[3]/a').click()
    #     # 点击银行卡管理
    #     time.sleep(2)
    #
    #     cards = driver.find_elements_by_xpath("//div[@class='row']/div[not(@class)]/div")
    #     # 银行卡的数量
    #
    #     cards[0].find_element_by_xpath('./div[@class="card"]//ul[@class="card-actions"]/li/button[contains(@class,"update_btn")]').click()
    #     # 对第一张银行卡执行编辑操作
    #     time.sleep(2)
    #
    #     ele = driver.find_element_by_xpath('//input[@id="cardBank_update_input"]')
    #     # 银行名称输入框
    #
    #     name1 = ele.get_attribute("value")
    #     # 初始银行名
    #
    #     ele.clear()
    #     # 清空银行名
    #
    #     name2 =  '中国交通银行'
    #     ele.send_keys(name2)
    #
    #     driver.find_element_by_xpath('//button[@id="bankCard_update_btn"]').click()
    #     # 点击更新按钮
    #
    #     time.sleep(3)
    #     cards = driver.find_elements_by_xpath("//div[@class='row']/div[not(@class)]/div")
    #     name3 = cards[0].find_element_by_xpath('./div/div[contains(@class,"card-header")]/div/h4').text
    #     # 修改之后的银行卡名
    #
    #     if name3.find(name1) != -1:
    #         # 修改之后的银行卡名 和 没有修改之前银行卡 进行比较
    #         print('通过')
    #     else:
    #         print('不通过')
    #         driver.save_screenshot('test_bankcard7.png')
    #
    #     assert name3.find(name1) != -1
    #
    #     driver.quit()


    # def test_bankcard8(self):
    #
    #     driver = webdriver.Chrome()
    #
    #     driver.get(url='http://localhost:90')
    #
    #     driver.maximize_window()
    #
    #     driver.find_element_by_xpath('//input[@id="username"]').send_keys("lisi")
    #
    #     driver.find_element_by_xpath('//input[@id="password"]').send_keys("123456")
    #
    #     driver.find_element_by_xpath('//button[@id="login_btn"]').click()
    #
    #     time.sleep(2)
    #
    #     driver.find_element_by_xpath('//nav[@class="sidebar-main"]/ul/li[4]').click()
    #
    #     time.sleep(1)
    #
    #     driver.find_element_by_xpath('//nav[@class="sidebar-main"]/ul/li[4]/ul/li[3]/a').click()
    #     # 点击银行卡管理
    #     time.sleep(2)
    #
    #     cards = driver.find_elements_by_xpath("//div[@class='row']/div[not(@class)]/div")
    #     # 银行卡的数量
    #
    #     cards[0].find_element_by_xpath('./div[@class="card"]//ul[@class="card-actions"]/li/button[contains(@class,"update_btn")]').click()
    #     # 对第一张银行卡执行编辑操作
    #     time.sleep(2)
    #
    #     ele = driver.find_element_by_xpath('//input[@id="cardNum_update_input"]')
    #     # 银行卡号输入框
    #
    #     name1 = ele.get_attribute("value")
    #     # 初始银行卡号
    #
    #     ele.clear()
    #     # 清空银行名
    #
    #     name2 = '1234567891054312'
    #     ele.send_keys(name2)
    #     # 输入新的银行卡号
    #     driver.find_element_by_xpath('//button[@id="bankCard_update_btn"]').click()
    #     # 点击更新按钮
    #
    #     time.sleep(3)
    #     cards = driver.find_elements_by_xpath("//div[@class='row']/div[not(@class)]/div")
    #     name3 = cards[0].find_element_by_xpath('./div/div[@class="card-body"]/h4[1]').text
    #     # 修改之后的银行卡号
    #
    #     if name3.find(name1) == -1:
    #         # 修改之后的银行卡号 和 没有修改之前银行卡号 进行比较
    #         print('通过')
    #     else:
    #         print('不通过')
    #         driver.save_screenshot('test_bankcard8.png')
    #
    #     assert name3.find(name1) == -1
    #
    #     driver.quit()


    # def test_bankcard9(self):
    #
    #     driver = webdriver.Chrome()
    #
    #     driver.get(url='http://localhost:90')
    #
    #     driver.maximize_window()
    #
    #     driver.find_element_by_xpath('//input[@id="username"]').send_keys("lisi")
    #
    #     driver.find_element_by_xpath('//input[@id="password"]').send_keys("123456")
    #
    #     driver.find_element_by_xpath('//button[@id="login_btn"]').click()
    #
    #     time.sleep(2)
    #
    #     driver.find_element_by_xpath('//nav[@class="sidebar-main"]/ul/li[4]').click()
    #
    #     time.sleep(1)
    #
    #     driver.find_element_by_xpath('//nav[@class="sidebar-main"]/ul/li[4]/ul/li[3]/a').click()
    #     # 点击银行卡管理
    #     time.sleep(2)
    #
    #     cards = driver.find_elements_by_xpath("//div[@class='row']/div[not(@class)]/div")
    #     # 银行卡的数量
    #
    #     cards[0].find_element_by_xpath('./div[@class="card"]//ul[@class="card-actions"]/li/button[contains(@class,"update_btn")]').click()
    #     # 对第一张银行卡执行编辑操作
    #     time.sleep(2)
    #
    #     ele = driver.find_element_by_xpath('//input[@id="cardNum_update_input"]')
    #     # 银行卡号输入框
    #
    #     name1 = ele.get_attribute("value")
    #     # 初始银行卡号
    #
    #     ele.clear()
    #     # 清空银行名
    #
    #     name2 = '12345678911234567' # 长度为 17
    #     ele.send_keys(name2)
    #     # 输入新的银行卡号
    #     driver.find_element_by_xpath('//button[@id="bankCard_update_btn"]').click()
    #     # 点击更新按钮
    #
    #     time.sleep(3)
    #     cards = driver.find_elements_by_xpath("//div[@class='row']/div[not(@class)]/div")
    #     name3 = cards[0].find_element_by_xpath('./div/div[@class="card-body"]/h4[1]').text
    #     # 修改之后的银行卡号
    #
    #     if name3.find(name1) != -1:
    #         # 修改之后的银行卡号 和 没有修改之前银行卡号 进行比较
    #         print('通过')
    #     else:
    #         print('不通过')
    #         driver.save_screenshot('test_bankcard9.png')
    #
    #     assert name3.find(name1) != -1
    #
    #     driver.quit()


    def test_bankcard10(self):

        driver = webdriver.Chrome()

        driver.get(url='http://localhost:90')

        driver.maximize_window()

        driver.find_element_by_xpath('//input[@id="username"]').send_keys("lisi")

        driver.find_element_by_xpath('//input[@id="password"]').send_keys("123456")

        driver.find_element_by_xpath('//button[@id="login_btn"]').click()

        time.sleep(2)

        driver.find_element_by_xpath('//nav[@class="sidebar-main"]/ul/li[4]').click()

        time.sleep(1)

        driver.find_element_by_xpath('//nav[@class="sidebar-main"]/ul/li[4]/ul/li[3]/a').click()
        # 点击银行卡管理
        time.sleep(2)

        cards = driver.find_elements_by_xpath("//div[@class='row']/div[not(@class)]/div")
        # 银行卡的数量

        test_card = None
        for card in cards:
            text = card.find_element_by_xpath('./div/div[1]/div/h4').text
            if text.find('借记卡') != -1:
                test_card = card
                break

        if test_card:

            edit_btn = test_card.find_element_by_xpath(
                './div[@class="card"]//ul[@class="card-actions"]/li/button[contains(@class,"update_btn")]')
            # 编辑操作
            id = edit_btn.get_attribute("update-id")
            edit_btn.click()

            time.sleep(2)

            driver.find_element_by_xpath('//div[@id="bankCardUpdateModal"]/div//form/div[2]/div[1]/div/label[2]/span').click()
            # 选择信用卡
            driver.find_element_by_xpath('//button[@id="bankCard_update_btn"]').click()
            # 点击更新按钮
            time.sleep(3)

            cards = driver.find_elements_by_xpath("//div[@class='row']/div[not(@class)]/div")

            test_card2 = None
            for card in cards:
                edit_btn = card.find_element_by_xpath('./div[@class="card"]//button[contains(@class,"update_btn")]')
                if edit_btn.get_attribute("update-id") == id:
                    test_card2 = card
                    break

            if test_card2:
                bankCard_name = test_card2.find_element_by_xpath('./div/div/div/h4').text
                if bankCard_name.find('信用卡') != -1:
                    print('通过')
                else:
                    print('不通过！')

                assert bankCard_name.find('信用卡') != -1

        else:
            print('没有借记卡！')


        driver.quit()
